package com.example.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.reggie.common.R;
import com.example.reggie.dto.DishDto;
import com.example.reggie.dto.SetmealDto;
import com.example.reggie.entity.*;
import com.example.reggie.service.SetmealService;
import com.example.reggie.service.impl.CategoryServiceImpl;
import com.example.reggie.service.impl.SetmealDishServiceImpl;
import com.example.reggie.service.impl.SetmealServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.asm.SpringAsmInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@RestController
@RequestMapping("/setmeal")
public class SetmealController {

    @Autowired
    SetmealServiceImpl setmealService;

    @Autowired
    SetmealDishServiceImpl setmealDishService;

    @Autowired
    CategoryServiceImpl categoryService;


    /**
     * 套餐新增
     * @param
     */
    @PostMapping()
    public R<String> save(@RequestBody SetmealDto setmealDto){
        log.info("新增套餐信息:{}",setmealDto.toString());
        setmealService.saveWithDish(setmealDto);
        return R.success("新增成功");
    }

    /**
     * 套餐分页查询
     * @param
     */
    @GetMapping("/page")
    public R<IPage> page(int page,int pageSize,String name){
        IPage<Setmeal> pageinfo=new Page<>(page,pageSize);
        LambdaQueryWrapper<Setmeal> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.like(name!=null,Setmeal::getName,name);
        setmealService.page(pageinfo,queryWrapper);
        IPage<SetmealDto>pageinfo1=new Page<>();
        BeanUtils.copyProperties(pageinfo,pageinfo1,"records");

        List<SetmealDto> collect = pageinfo.getRecords().stream().map((item) -> {
            SetmealDto setmealDto = new SetmealDto();
            BeanUtils.copyProperties(item, setmealDto);
            Long id = setmealDto.getId();
            LambdaQueryWrapper<SetmealDish> queryWrapper1 = new LambdaQueryWrapper<>();
            queryWrapper1.eq(id != null, SetmealDish::getSetmealId, id);
            List<SetmealDish> list = setmealDishService.list(queryWrapper1);
            setmealDto.setSetmealDishes(list);
            Long categoryId = setmealDto.getCategoryId();
            Category byId = categoryService.getById(categoryId);
            setmealDto.setCategoryName(byId.getName());
            return setmealDto;
        }).collect(Collectors.toList());

        pageinfo1.setRecords(collect);
        return R.success(pageinfo1);
    }

    /**
     * 查询一个套餐
     *
     * @param
     */
    @GetMapping("/{id}")
    public R<SetmealDto> get(@PathVariable Long id) {
        Setmeal setmeal = setmealService.getById(id);
        SetmealDto setmealDto=new SetmealDto();
        BeanUtils.copyProperties(setmeal, setmealDto);
        Long setmealDtoId = setmealDto.getId();
        LambdaQueryWrapper<SetmealDish>queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(setmealDtoId!=null,SetmealDish::getSetmealId,id);
        List<SetmealDish> list = setmealDishService.list(queryWrapper);
        setmealDto.setSetmealDishes(list);
        Category byId = categoryService.getById(setmealDto.getCategoryId());
        setmealDto.setCategoryName(byId.getName());
        return R.success(setmealDto);
    }


    /**
     * 修改一个套餐以及关联的dto setmeal_dish
     *
     * @param
     */
    @PutMapping()
    public R<String> update(@RequestBody SetmealDto setmealDto) {
        log.info("SetmealDto展示:{}", setmealDto.toString());
        boolean b = setmealService.updateWithFlavor(setmealDto);
        if (!b) {
            return R.error("修改失败");
        }
        return R.success("修改成功");
    }

    /**
     * 批量/单个+停售/启售套餐
     * 0->停售
     * 1->启售
     * @param
     */
    @PostMapping("/status/{status}")
    public R<String> updateWithStatus(String ids, @PathVariable int status){
        log.info("套餐id:{},想要改成:{}",ids,status);
        boolean b =setmealService.updateWithStatus(ids,status);
        if (!b){
            return R.error("修改失败");
        }
        return R.success("修改成功");
    }

    /**
     * 删除套餐
     * @param
     */
    @DeleteMapping()
    public R<String> del(String ids){
        log.info("想要删除的id:{}",ids);
        boolean b=setmealService.delMulit(ids);
        return R.success("删除成功");
    }

    /**
     * 展示指定id的套餐
     * @param
     */
    @GetMapping("/list")
    public R<List<Setmeal>> list(Setmeal setmeal){
        log.info("想要展示的指定套餐setmeal:{}",setmeal);
        LambdaQueryWrapper<Setmeal>queryWrapper=new LambdaQueryWrapper<>();
        // select * from setmeal where categoryid = ? and status = 1 ;
        queryWrapper.eq(setmeal.getCategoryId()!=null,Setmeal::getCategoryId,setmeal.getCategoryId());
        queryWrapper.eq(setmeal.getStatus()!=null,Setmeal::getStatus,setmeal.getStatus());
        List<Setmeal> list = setmealService.list(queryWrapper);
        if (list==null){
            return R.error("查询失败");
        }
        return R.success(list);
    }

    /**
     * 展示指定id的套餐中的dish
     * @param
     */
    @GetMapping("/dish/{setmealId}")
    public R<SetmealDto> dish(@PathVariable Long setmealId){
        //套餐id
        log.info("指定category的dish:{}",setmealId);
        Setmeal setmeal = setmealService.getById(setmealId);
        SetmealDto setmealDto = new SetmealDto();
        BeanUtils.copyProperties(setmeal,setmealDto);

        Long categoryId = setmealDto.getCategoryId();
        Category byId = categoryService.getById(categoryId);
        String categoryname = byId.getName();

        LambdaQueryWrapper<SetmealDish>queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(setmealId!=null,SetmealDish::getSetmealId,setmealId);
        List<SetmealDish> list = setmealDishService.list(queryWrapper);

        setmealDto.setSetmealDishes(list);
        setmealDto.setCategoryName(categoryname);

        log.info("setmealDto:{}",setmealDto);
        log.info("setmealDto的Image地址:{}",setmealDto.getImage());
        return R.success(setmealDto);
    }

}
